<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['shipment'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ t('xmwms.outboundOrder.shipment') }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    v-if="!baTable.form.loading"
                    :model="items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('xmwms.outboundOrder.courier_number')"
                        type="string"
                        v-model="items.courier_number"
                        prop="courier_number"
                        :placeholder="t('Please input field', { field: t('xmwms.outboundOrder.courier_number') })"
                    />
                    <FormItem
                        :label="t('xmwms.outboundOrder.carrier_id')"
                        type="remoteSelect"
                        v-model="items.carrier_id"
                        prop="carrier_id"
                        :input-attr="{ multiple: false, pk: 'carrier.id', field: 'name', 'remote-url': '/admin/xmwms.Carrier/index' }"
                        :placeholder="t('Please select field', { field: t('xmwms.outboundOrder.carrier_id') })"
                    />
                    <FormItem
                        :label="t('xmwms.outboundOrder.shipment_time')"
                        type="datetime"
                        v-model="items.shipment_time"
                        prop="shipment_time"
                        :placeholder="t('Please select field', { field: t('xmwms.outboundOrder.shipment_time') })"
                    />
                    <FormItem
                        :label="t('xmwms.outboundOrder.remark')"
                        type="textarea"
                        v-model="items.remark"
                        prop="remark"
                        :placeholder="t('Please input field', { field: t('xmwms.outboundOrder.remark') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="onAddShipmentSubmit(formRef)" type="primary">
                    {{ t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, inject, watch, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormRules } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { outboundOrderShipment } from '/@/api/backend/xmwms/outboundOrder'
import { ElNotification } from 'element-plus'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const { t } = useI18n()
const items = reactive({
    outbound_order_id: 0,
    courier_number: '',
    carrier_id: 0,
    shipment_time: '',
    remark: '',
})

const rules = reactive<FormRules>({
    courier_number: [buildValidatorData({ name: 'required', title: t('xmwms.outboundOrder.courier_number') })],
    carrier_id: [buildValidatorData({ name: 'required', title: t('xmwms.outboundOrder.carrier_id') })],
    shipment_time: [buildValidatorData({ name: 'required', title: t('xmwms.outboundOrder.shipment_time') })],
})

const onAddShipmentSubmit = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            // 验证通过
            outboundOrderShipment(
                {
                    courier_number: items.courier_number,
                    carrier_id: items.carrier_id,
                    shipment_time: items.shipment_time,
                    remark: items.remark,
                },
                items.outbound_order_id
            )
                .then((res) => {
                    ElNotification({
                        message: res.msg,
                        type: 'success',
                    })
                    baTable.form.operate = ''
                    baTable.onTableHeaderAction('refresh', {})
                })
                .finally(() => {
                    baTable.form.extend!.submitLoading = false
                })
        }
    })
}

watch(
    () => baTable.form.extend!.shipment,
    (newVal) => {
        items.outbound_order_id = newVal.outbound_order_id
        items.courier_number = newVal.courier_number
        items.carrier_id = newVal.carrier_id
        items.shipment_time = newVal.shipment_time
        items.remark = newVal.remark
    }
)
</script>

<style scoped lang="scss"></style>
